<script lang="ts" setup name="UserManage">
import { CirclePlus, Delete, EditPen, Refresh, View } from '@element-plus/icons-vue'
import { ProTableInstance } from '~/library/components/ProTable/interface'
import { User } from '~/src/api/interface/user'
import { createUserMangerAPI, deleteUserMangerAPI, editUserMangerAPI, getUserMangerAPI, resetPasswordAPI } from '~/src/api/user'
import UserMangerDialog from '../components/UserMangerDialog.vue'
import { userMaNgerColumns } from './info/columns'
const proTableRef = ref<ProTableInstance>()

// 删除
const deleteAccount = async (params: User.UserManger) => {
  await useHandleData(deleteUserMangerAPI, { id: params.id }, `删除【${params.nickName}】用户`)
  proTableRef.value?.getTableList()
}

const resetPassword = async (params: User.UserManger) => {
  await useHandleData(resetPasswordAPI, { id: params.id }, `重置【${params.nickName}】密码`)
  // proTable.value?.getTableList();
}

const dialogRef = ref<InstanceType<typeof UserMangerDialog>>()
const openDrawer = (title: string, row?: User.UserManger) => {
  const params = {
    title,
    isView: title === '查看',
    row: { ...row },
    api: title === '新增' ? createUserMangerAPI : title === '编辑' ? editUserMangerAPI : undefined,
    getTableList: proTableRef.value?.getTableList,
  }
  dialogRef.value?.acceptParams(params)
}
</script>

<template>
  <div class="auto-height-container">
    <pro-table ref="proTableRef" :columns="userMaNgerColumns" :request-api="getUserMangerAPI" title="用户列表">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'add'" :icon="CirclePlus" type="primary" @click="openDrawer('新增')">新增用户</el-button>
      </template>
      <!-- Expand -->
      <template #expand="scope">
        {{ scope.row }}
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-dropdown trigger="hover">
          <el-button type="primary">
            操作菜单
            <el-icon><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>
                <el-button v-auth="'view'" :icon="View" link type="primary" @click="openDrawer('查看', scope.row)">查看</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-auth="'edit'" :icon="EditPen" link type="primary" @click="openDrawer('编辑', scope.row)">编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-auth="'delete'" :icon="Delete" link type="primary" @click="deleteAccount(scope.row)">删除</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-auth="'reSet'" :icon="Refresh" link type="primary" @click="resetPassword(scope.row)">重置密码</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </template>
    </pro-table>
    <user-manger-dialog ref="dialogRef" />
  </div>
</template>

<style lang="scss" scoped></style>
